/*
  Copyright 2011 Jonathan Albarrán León

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package com.jaal.movilconsultanet.activities;

import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.google.ads.AdRequest;
import com.google.ads.AdView;
import com.jaal.movilconsultanet.R;
import com.jaal.movilconsultanet.Util;
import com.jaal.movilconsultanet.XMLContent;
import com.jaal.movilconsultanet.asynctasks.GSMSessionFinalizer;
import com.jaal.movilconsultanet.asynctasks.PerfilRequester;
import com.jaal.movilconsultanet.asynctasks.SaldoRequester;

public class Consulta extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.consulta);
		configureUI();
		
		AdView adView = (AdView) findViewById(R.id.adView1);
		AdRequest request = new AdRequest();
	    
	    request.addTestDevice(AdRequest.TEST_EMULATOR);
	    
		adView.loadAd(request);
		
	}

	private void configureUI() {
		refrescarPerfil();
		refrescarSaldo();
	}

	public String obtenerSessionId() {
		Map<String, String> mapa = XMLContent.getXML("sesiones");

		String sessionId = null;
		if (mapa != null) {
			sessionId = mapa.get("session_id");
		}
		return sessionId;
	}

	private void actualizarSaldo() {
		new SaldoRequester(Consulta.this).execute(obtenerSessionId());
	}

	private void actualizarPerfil() {
		new PerfilRequester(Consulta.this).execute(obtenerSessionId());
	}

	private void endSession() {
		new GSMSessionFinalizer(Consulta.this).execute(obtenerSessionId());
	}

	private void acercaDe() {
		Intent i = new Intent(this, About.class);
		startActivity(i);
	}

	public void refrescarPerfil() {
		Log.d("MCN", "refrescarPerfil - inicio");
		Map<String, String> mMap;
		mMap = XMLContent.getXML("perfil");

		if (mMap == null) {
			Toast.makeText(this, getResources().getText(R.string.no_profile),
					Toast.LENGTH_SHORT).show();
			return;
		}

		Log.d("MCN", "refrescarPerfil - map: " + mMap);

		TextView lastModTxt = (TextView) findViewById(R.id.profile_last_modifified);
		lastModTxt.setText(mMap.get("last_modified"));
		Log.d("MCN", "refrescarPerfil - get: last_modified");

		TextView perfilTxt = (TextView) findViewById(R.id.profile_number);
		perfilTxt.setText(mMap.get("movil"));
		Log.d("MCN", "refrescarPerfil - get: movil");

		perfilTxt = (TextView) findViewById(R.id.profile_client);
		perfilTxt.setText(mMap.get("cliente"));
		Log.d("MCN", "refrescarPerfil - get: cliente");

		perfilTxt = (TextView) findViewById(R.id.profile_identification);
		perfilTxt.setText(mMap.get("cedula"));
		Log.d("MCN", "refrescarPerfil - get: cedula");

		perfilTxt = (TextView) findViewById(R.id.profile_status);
		perfilTxt.setText(mMap.get("estatus"));
		Log.d("MCN", "refrescarPerfil - get: estatus");

		perfilTxt = (TextView) findViewById(R.id.profile_plan);
		perfilTxt.setText(mMap.get("plan"));
		Log.d("MCN", "refrescarPerfil - get: plan");

		perfilTxt = (TextView) findViewById(R.id.profile_deviceType);
		perfilTxt.setText(mMap.get("tipo_equipo"));
		Log.d("MCN", "refrescarPerfil - get: tipo_equipo");

		perfilTxt = (TextView) findViewById(R.id.profile_model);
		perfilTxt.setText(mMap.get("modelo_telefono"));
		Log.d("MCN", "refrescarPerfil - get: modelo_telefono");

		perfilTxt = (TextView) findViewById(R.id.profile_description);
		perfilTxt.setText(mMap.get("descripcion"));
		Log.d("MCN", "refrescarPerfil - get: descripcion");

		Log.d("MCN", "refrescarPerfil - fin");
	}

	private boolean checkSaldoVisibility(Map<String, String> values) {
		boolean visible = true;
		if (values.get("disponible").equalsIgnoreCase(Util.UNKNOWN_ERROR) || values.get("disponible").equalsIgnoreCase("0")) {
			visible = false;
		} else {
			visible = true;
		}
		
		if (values.get("total").equalsIgnoreCase(Util.UNKNOWN_ERROR) || values.get("total").equalsIgnoreCase("0")) {
			visible = false;
		} else {
			visible = true;
		}
		return visible;
	}
	
	public void refrescarSaldo() {
		Log.d("MCN", "refrescarSaldo - inicio");
		Map<String, Map<String, String>> mMap;
		mMap = XMLContent.getXMLTree("saldo");

		if (mMap == null) {
			Toast.makeText(this, getResources().getText(R.string.no_saldo),
					Toast.LENGTH_SHORT).show();
			return;
		}

		Log.d("MCN", "refrescarSaldo - map: " + mMap);

		TextView lastModTxt = (TextView) findViewById(R.id.account_last_modified);
		lastModTxt.setText(mMap.get("last_modified").get("last_modified"));
		Log.d("MCN", "refrescarSaldo - get: last_modified");

		TextView saldoTxt = null;

		// balance
		String row = "balance";
		if (checkSaldoVisibility(mMap.get(row))) {

			TableRow tr = (TableRow) findViewById(R.id.account_balance_row);
			tr.setVisibility(View.VISIBLE);

			saldoTxt = (TextView) findViewById(R.id.account_balance_disponible);
			saldoTxt.setText("Disponible: " + mMap.get(row).get("disponible"));

			saldoTxt = (TextView) findViewById(R.id.account_balance_total);
			saldoTxt.setText("Total: " + mMap.get(row).get("total"));

			saldoTxt = (TextView) findViewById(R.id.account_balance_unidad);
			saldoTxt.setText("Unidad: " + mMap.get(row).get("unidad"));

			saldoTxt = (TextView) findViewById(R.id.account_balance_fecha);
			saldoTxt.setText("Expira: " + mMap.get(row).get("fecha"));
		} else {
			TableRow tr = (TableRow) findViewById(R.id.account_balance_row);
			tr.setVisibility(View.GONE);
		}
		Log.d("MCN", "refrescarSaldo - get: balance");

		// sms_pack
		row = "sms_pack";
		if (checkSaldoVisibility(mMap.get(row))) {
			TableRow tr = (TableRow) findViewById(R.id.account_smsPack_row);
			tr.setVisibility(View.VISIBLE);
			saldoTxt = (TextView) findViewById(R.id.account_smsPack_disponible);
			saldoTxt.setText("Disponible: " + mMap.get(row).get("disponible"));

			saldoTxt = (TextView) findViewById(R.id.account_smsPack_total);
			saldoTxt.setText("Total: " + mMap.get(row).get("total"));

			saldoTxt = (TextView) findViewById(R.id.account_smsPack_unidad);
			saldoTxt.setText("Unidad: " + mMap.get(row).get("unidad"));

			saldoTxt = (TextView) findViewById(R.id.account_smsPack_fecha);
			saldoTxt.setText("Expira: " + mMap.get(row).get("fecha"));
		} else {
			TableRow tr = (TableRow) findViewById(R.id.account_smsPack_row);
			tr.setVisibility(View.GONE);
		}
		Log.d("MCN", "refrescarSaldo - get: sms_pack");

		// voice_seconds
		row = "voice_seconds";
		if (checkSaldoVisibility(mMap.get(row))) {
			TableRow tr = (TableRow) findViewById(R.id.account_voicePlanSeconds_row);
			tr.setVisibility(View.VISIBLE);
			saldoTxt = (TextView) findViewById(R.id.account_voicePlanSeconds_disponible);
			saldoTxt.setText("Disponible: " + mMap.get(row).get("disponible"));

			saldoTxt = (TextView) findViewById(R.id.account_voicePlanSeconds_total);
			saldoTxt.setText("Total: " + mMap.get(row).get("total"));

			saldoTxt = (TextView) findViewById(R.id.account_voicePlanSeconds_unidad);
			saldoTxt.setText("Unidad: " + mMap.get(row).get("unidad"));

			saldoTxt = (TextView) findViewById(R.id.account_voicePlanSeconds_fecha);
			saldoTxt.setText("Expira: " + mMap.get(row).get("fecha"));
		} else {
			TableRow tr = (TableRow) findViewById(R.id.account_voicePlanSeconds_row);
			tr.setVisibility(View.GONE);
		}
		Log.d("MCN", "refrescarSaldo - get: voice_seconds");

		// asterisk_sms
		row = "asterisk_sms";
		if (checkSaldoVisibility(mMap.get(row))) {
			TableRow tr = (TableRow) findViewById(R.id.account_asteriskSms_row);
			tr.setVisibility(View.VISIBLE);
			saldoTxt = (TextView) findViewById(R.id.account_asteriskSms_disponible);
			saldoTxt.setText("Disponible: " + mMap.get(row).get("disponible"));

			saldoTxt = (TextView) findViewById(R.id.account_asteriskSms_total);
			saldoTxt.setText("Total: " + mMap.get(row).get("total"));

			saldoTxt = (TextView) findViewById(R.id.account_asteriskSms_unidad);
			saldoTxt.setText("Unidad: " + mMap.get(row).get("unidad"));

			saldoTxt = (TextView) findViewById(R.id.account_asteriskSms_fecha);
			saldoTxt.setText("Expira: " + mMap.get(row).get("fecha"));
		} else {
			TableRow tr = (TableRow) findViewById(R.id.account_asteriskSms_row);
			tr.setVisibility(View.GONE);
		}
		Log.d("MCN", "refrescarSaldo - get: voice_seconds");

		// voice_sms
		row = "voice_sms";
		if (checkSaldoVisibility(mMap.get(row))) {
			TableRow tr = (TableRow) findViewById(R.id.account_voicePlanSms_row);
			tr.setVisibility(View.VISIBLE);
			saldoTxt = (TextView) findViewById(R.id.account_voicePlanSms_disponible);
			saldoTxt.setText("Disponible: " + mMap.get(row).get("disponible"));

			saldoTxt = (TextView) findViewById(R.id.account_voicePlanSms_total);
			saldoTxt.setText("Total: " + mMap.get(row).get("total"));

			saldoTxt = (TextView) findViewById(R.id.account_voicePlanSms_unidad);
			saldoTxt.setText("Unidad: " + mMap.get(row).get("unidad"));

			saldoTxt = (TextView) findViewById(R.id.account_voicePlanSms_fecha);
			saldoTxt.setText("Expira: " + mMap.get(row).get("fecha"));
		} else {
			TableRow tr = (TableRow) findViewById(R.id.account_voicePlanSms_row);
			tr.setVisibility(View.GONE);
		}
		Log.d("MCN", "refrescarSaldo - get: voice_sms");

		// promo_sms
		row = "promo_sms";

		if (checkSaldoVisibility(mMap.get(row))) {

			TableRow tr = (TableRow) findViewById(R.id.account_promoSms_row);
			tr.setVisibility(View.VISIBLE);

			saldoTxt = (TextView) findViewById(R.id.account_promoSms_disponible);
			saldoTxt.setText("Disponible: " + mMap.get(row).get("disponible"));

			saldoTxt = (TextView) findViewById(R.id.account_promoSms_total);
			saldoTxt.setText("Total: " + mMap.get(row).get("total"));

			saldoTxt = (TextView) findViewById(R.id.account_promoSms_unidad);
			saldoTxt.setText("Unidad: " + mMap.get(row).get("unidad"));

			saldoTxt = (TextView) findViewById(R.id.account_promoSms_fecha);
			saldoTxt.setText("Expira: " + mMap.get(row).get("fecha"));

		} else {
			TableRow tr = (TableRow) findViewById(R.id.account_promoSms_row);
			tr.setVisibility(View.GONE);
		}

		Log.d("MCN", "refrescarSaldo - get: promo_sms");

		// data
		row = "data";
		if (checkSaldoVisibility(mMap.get(row))) {

			TableRow tr = (TableRow) findViewById(R.id.account_dataServices_row);
			tr.setVisibility(View.VISIBLE);
			saldoTxt = (TextView) findViewById(R.id.account_dataServices_disponible);
			saldoTxt.setText("Disponible: " + mMap.get(row).get("disponible"));

			saldoTxt = (TextView) findViewById(R.id.account_dataServices_total);
			saldoTxt.setText("Total: " + mMap.get(row).get("total"));

			saldoTxt = (TextView) findViewById(R.id.account_dataServices_unidad);
			saldoTxt.setText("Unidad: " + mMap.get(row).get("unidad"));

			saldoTxt = (TextView) findViewById(R.id.account_dataServices_fecha);
			saldoTxt.setText("Expira: " + mMap.get(row).get("fecha"));
		} else {
			TableRow tr = (TableRow) findViewById(R.id.account_dataServices_row);
			tr.setVisibility(View.GONE);
		}
		Log.d("MCN", "refrescarSaldo - get: data");

		// data
		row = "voice_data";
		if (checkSaldoVisibility(mMap.get(row))) {

			TableRow tr = (TableRow) findViewById(R.id.account_voicePlanDataServices_row);
			tr.setVisibility(View.VISIBLE);
			saldoTxt = (TextView) findViewById(R.id.account_voicePlanDataServices_disponible);
			saldoTxt.setText("Disponible: " + mMap.get(row).get("disponible"));

			saldoTxt = (TextView) findViewById(R.id.account_voicePlanDataServices_total);
			saldoTxt.setText("Total: " + mMap.get(row).get("total"));

			saldoTxt = (TextView) findViewById(R.id.account_voicePlanDataServices_unidad);
			saldoTxt.setText("Unidad: " + mMap.get(row).get("unidad"));

			saldoTxt = (TextView) findViewById(R.id.account_voicePlanDataServices_fecha);
			saldoTxt.setText("Expira: " + mMap.get(row).get("fecha"));
			Log.d("MCN", "refrescarSaldo - get: voice_data");
		} else {
			TableRow tr = (TableRow) findViewById(R.id.account_voicePlanDataServices_row);
			tr.setVisibility(View.GONE);
		}
		Log.d("MCN", "refrescarSaldo - fin");
	}

	// MENU

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.consulta_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.cerrar_sesion:
			endSession();
			return true;
		case R.id.actualizar_perfil:
			if (Util.isOnline()) {
				actualizarPerfil();
			}
			return true;
		case R.id.actualizar_saldo:
			if (Util.isOnline()) {
				actualizarSaldo();
			}
			return true;
		case R.id.show_about:
			acercaDe();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}
